package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.edu.nuc.wangchao.modle.FollowsInfo;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RelationListAdapter extends BaseAdapter {
	private Context context = null;
	private List<FollowsInfo> followsInfos = null;
	private AsyncImageLoader loader = new AsyncImageLoader();
	private Map<Integer, View> viewMaps = new HashMap<Integer, View>();

	public RelationListAdapter(List<FollowsInfo> followsInfos, Context context) {
		super();
		this.followsInfos = followsInfos;
		this.context = context;
	}

	@Override
	public int getCount() {
		return followsInfos.size();
	}

	@Override
	public Object getItem(int position) {
		return followsInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = viewMaps.get(position);
		if (rowView == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.followitem, null);

			TextView nameView = (TextView) rowView
					.findViewById(R.id.followNameId);
			TextView nickView = (TextView) rowView
					.findViewById(R.id.followNickId);
			final ImageView imageView = (ImageView) rowView
					.findViewById(R.id.followIconId);
			FollowsInfo u = (FollowsInfo) getItem(position);
			nameView.setText(u.getName());
			nickView.setText(u.getNick());
//			String url = u.getHead();
//			Drawable drawable = loader.loadDrawable(url + "/50",
//					new AsyncImageLoader.ImageCallback() {
//						@Override
//						public void imageLoaded(Drawable imageDrawable) {
//							imageView.setImageDrawable(imageDrawable);
//						}
//					});

			if (!u.getHead().equals("")) {
				Drawable headDrawable = loader.loadDrawable(
						u.getHead() + "/50",
						new AsyncImageLoader.ImageCallback() {

							@Override
							public void imageLoaded(Drawable imageDrawable) {
								imageView.setImageDrawable(imageDrawable);
							}
						});
				if (headDrawable != null) {
					imageView.setImageDrawable(headDrawable);
				}
			} else {
				System.out.println(imageView);
				imageView.setImageResource(R.drawable.icon);
			}
			viewMaps.put(position, rowView);
		}
		return rowView;
	}
}
